import React from "react";
import {Button, Col, Input, Row, Form} from "antd";
import BaseComponent from "../../../../redare/components/BaseComponent";
import PlatformPresenter from "../../.gen/presenter/component/core/PlatformPresenter";
import {getLoginUserInfo} from "../../utils/WorkbenchUtil";

export default class RoleForm extends BaseComponent {

    static defaultProps = {
        platform: undefined,
        onAddSuccess: undefined,
        onEditSuccess: undefined
    };

    constructor(props) {
        super(props);
        this.state = {data: {}};
        this.presenter = new PlatformPresenter(this);
        this._user = getLoginUserInfo();
    }

    componentDidMount() {
        if (this.props.id) {
            this.presenter.getRole({id: this.props.id}, {}, (result) => {
                this.form.setFieldsValue(result.data);
                this.setState({data: result.data});
            });
        }
    }

    _closeForm() {
        if (this.props.onCancel) {
            this.props.onCancel();
        }
    }

    _onSubmit() {
        this.form.validateFields().then(values => {
            this.showDoingIndicator('处理中');
            if (this.props.id) {
                this.presenter.modifyRole({id: this.props.id, ...values}, {}, () => {
                    this.noticeSuccess("编辑成功");
                    if (this.props.onEditSuccess) {
                        this.props.onEditSuccess();
                    }
                });
            } else {
                this.presenter.addRole({...values, platform: this.props.platform}, {}, () => {
                    this.noticeSuccess("添加成功");
                    if (this.props.onAddSuccess) {
                        this.props.onAddSuccess();
                    }
                });
            }
        });
    }

    showLoadingIndicator(text) {
        this.showSpin(text);
    }

    closeLoadingIndicator() {
        this.closeSpin();
    }

    showDoingIndicator(text) {
        this.showSpin(text);
    }

    closeDoingIndicator() {
        this.closeSpin();
    }

    render() {
        const formItemLayout = {
            labelCol: {
                span: 4
            },
            wrapperCol: {
                span: 20
            },
        };
        return this.renderSpin(
            <Form ref={(o) => this.form = o}
                  className="form" {...formItemLayout}>
                <Form.Item label="角色名称"
                           name="name"
                           rules={[{required: true, message: '请输入角色名称！'}]}>
                    <Input placeholder="请输入角色名称"/>
                </Form.Item>
                <Form.Item label="角色描述" name="des">
                    <Input.TextArea placeholder="请输入角色描述"/>
                </Form.Item>
                <Row type="flex" justify="end">
                    <Col>
                        <Button onClick={this._closeForm.bind(this)}>
                            取消
                        </Button>
                    </Col>
                    <Col offset={1}>
                        <Button type="primary" onClick={this._onSubmit.bind(this)}>
                            保存
                        </Button>
                    </Col>
                </Row>
            </Form>
        );
    }
}
